//
//  UpLoadImgCell.m
//  PropertyMananger
//
//  Created by Mansion on 17/1/19.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "UpLoadImgCell.h"

@implementation UpLoadImgCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{

    static NSString *cellID = @"UpLoadImgCellID";
    UpLoadImgCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        cell = [[UpLoadImgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    //选中cell的颜色 -无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.imageViews = [[NSMutableArray alloc]init];
        
        self.titleLabel = [[UILabel alloc]init];
        [self.contentView addSubview: self.titleLabel];
         self.titleLabel.text = @"上传图片";
        
         self.titleLabel.textColor = BHColor(102, 102, 102);
         self.titleLabel.textAlignment = NSTextAlignmentLeft;
         self.titleLabel.font = BHFont(15);
        
        [ self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).with.offset(15);
            make.top.equalTo(self).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 25));
        }];
        
        self.lineView = [[UIView alloc]init];
        [self.contentView addSubview:self.lineView];
        self.lineView.backgroundColor = BHGrayColor;
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.contentView).with.offset(15);
            make.bottom.equalTo(self.contentView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 15, 1));
        }];

        
//        for (int i = 0; i<3; i++) {
//            
//            self.upLoadImgView = [[UIImageView alloc]init];
//            [self.contentView addSubview:self.upLoadImgView];
//            
//            self.upLoadImgView.image = IMG(@"bg_home_banner");
//            
//            [self.upLoadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.leading.equalTo(self).with.offset(15 + BHscreenW/3*i );
//                make.top.equalTo(titleLabel.mas_bottom).with.offset(5);
//                make.size.mas_equalTo(CGSizeMake(BHscreenW/3 - 30, 75));
//            }];
//           
//        }
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self.contentView addSubview:scrollView];
        
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.contentView).with.offset(0);
            make.top.equalTo(self.contentView).with.offset(30);
            make.size.mas_equalTo(CGSizeMake(BHscreenW, 100));
        }];

    
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, CGRectGetMaxY([self.imageViews.lastObject frame]));
        
    }
    return self;
}
- (void)imageViewTapped:(UITapGestureRecognizer *)tap{
    
    DLog(@"点击图片放大");
    UIImageView *imageView = (UIImageView *)tap.view;
    //show
    //传入所有的url、imageView和点击的index
    MQPhotosBrowser *photosView = [MQPhotosBrowser photoViewWithUrls:self.urlsBig sourceViews:self.imageViews andCurrentIndex:imageView.tag-1000];
    [photosView show];
    
//    DLog(@"%@ === %@",self.urlsBig,self.urlsSmall);
//    DLog(@"第%ld张",imageView.tag);
//    DLog(@"imageViews===%@",self.imageViews);
    
}
- (void)setUrlsBig:(NSArray *)urlsBig{

    _urlsBig = urlsBig;
    DLog(@"===%@",urlsBig);
    for (int i = 0; i < self.urlsBig.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.frame = CGRectMake(BHscreenW/3*i + 15 , CGRectGetMaxY( self.titleLabel.frame)+40, BHscreenW/3 - 30, 75);
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [self.contentView addSubview:imageView];
        
        //image
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.urlsSmall[i]]];
        //   imageView.image = IMG(@"bg_home_banner");
        
        //tap
        imageView.userInteractionEnabled = YES;
        imageView.tag = i+1000;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapped:)];
        [imageView addGestureRecognizer:tap];
        
        [self.imageViews addObject:imageView];
        self.upLoadImgView = imageView;
    }


}
- (void)setUrlsSmall:(NSArray *)urlsSmall{
  _urlsSmall = urlsSmall;
    for (int i = 0; i< self.urlsSmall.count; i++) {
        
        UIImageView * imageView = (UIImageView *)[self.contentView viewWithTag:i+1000];
         [imageView sd_setImageWithURL:[NSURL URLWithString:self.urlsSmall[i]]];
        
    }
   
}


@end
